package code.google.gui.fragments.search;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import code.google.controller.interfaces.ISearchController;
import code.google.gui.fragments.RegistrateableFragment;
import code.google.gui.fragments.adapter.SearchResultAdapter;
import code.google.gui.fragments.interfaces.IPlayerDispenser;
import code.google.gui.fragments.interfaces.ISearchDispenser;
import code.google.gui.fragments.interfaces.ISearchListener;
import code.google.gui.fragments.interfaces.ISetFragmentListener;
import code.google.gui.interfaces.ISearchFragment;
import code.google.model.util.interfaces.ISong;

import com.example.metamusicgui.R;

public abstract class SearchFragment extends RegistrateableFragment implements ISearchListener, ISearchFragment {
	
	protected LinearLayout rootView;
	protected EditText queryText;
	protected Button searchButton;
	protected ListView listView;
	protected ISearchController controller;
	
	private ISearchDispenser searchDispenser;
	private SearchResultAdapter lastAdapter;
	private String lastQuery;
	private IPlayerDispenser playerDispenser;
	private ISetFragmentListener setFragmentListener;
	private Activity context;
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		if(activity instanceof ISearchDispenser){
			searchDispenser = (ISearchDispenser) activity;
			searchDispenser.addSearchListener(this);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		rootView = (LinearLayout) inflater.inflate(R.layout.fragment_search, container, false);
		queryText = (EditText) rootView.findViewById(R.id.search_edittext);
		searchButton = (Button) rootView.findViewById(R.id.search_button);
		listView = (ListView) rootView.findViewById(R.id.search_listview);

		searchButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String query = queryText.getText().toString();
				searchDispenser.dispenseSearch(query);
				controller.doSearch(query);
			}
		});
		
		
		
		if(lastAdapter != null){
			setSearchResultAdapter(lastAdapter);
		}
		
		if(lastQuery != null){
			queryText.setText(lastQuery);
		}
		
		return rootView;
	}
	
	protected abstract ISearchController getController();

	@Override
	public void onSearch(String query) {
		lastQuery = query;
		queryText.setText(query);
	}

	@Override
	public void setSearchResultAdapter(SearchResultAdapter adapter) {
		lastAdapter = adapter;
		listView.setAdapter(adapter);		
	}

	@Override
	public Activity getContext() {
		return context;
	}
	
	@Override
	public void registrateFragmentAt(Activity context){
		this.context = context;
		if(context instanceof ISearchDispenser){
			searchDispenser = (ISearchDispenser) context;
			searchDispenser.addSearchListener(this);
		}
		if(context instanceof IPlayerDispenser){
			playerDispenser = (IPlayerDispenser)context;
		}
		if(context instanceof ISetFragmentListener)
			setFragmentListener = (ISetFragmentListener) context;
		controller = getController();
	}

	@Override
	public void deregistrateFragmentFrom(Activity context) {
		searchDispenser.removeSearchListener(this);
		searchDispenser = null;
		playerDispenser = null;
		setFragmentListener = null;
		this.context = null;
	}

	@Override
	public void dispensePlay(ISong song) {
		playerDispenser.dispensePlay(song);
		setFragmentListener.setCurrentFragment(ISetFragmentListener.PLAYER_FRAGMENT);
	}
}
